package com.geh.learn.stream;

import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class MapFlatMapTest {

    public static void main(String[] args) {

        StudentEntity stu1 = new StudentEntity("stu1", "pass1", "0", 18);
        StudentEntity stu2 = new StudentEntity("stu2", "pass2", "0", 23);
        StudentEntity stu3 = new StudentEntity("stu3", "pass3", "1", 24);
        StudentEntity stu4 = new StudentEntity("stu4", "pass4", "0", 27);
        StudentEntity stu5 = new StudentEntity("stu5", "pass5", "1", 16);

        List<StudentEntity> list1 = Lists.newArrayList(stu1, stu2, stu3, stu4, stu5);

        // 转换成自己类型的Map
        Map<String, StudentEntity> map1 = list1.stream().collect(Collectors.toMap(StudentEntity::getUsername, Function.identity()));
        System.out.println(map1);

        // 从Map得到List
        List<StudentEntity> listConvert = new ArrayList<>(map1.values());
        System.out.println(listConvert);

        // 得到别的类型的List
        List<StudentInfo> listOtherConvert = map1.values().stream().map(MapFlatMapTest::convertStuInfo).collect(Collectors.toList());
        System.out.println(listOtherConvert);

        // 转换成其他类型的Map
        Map<String, StudentInfo> map2 = list1.stream().map(MapFlatMapTest::convertStuInfo).collect(Collectors.toMap(StudentInfo::getUsername, Function.identity()));
        System.out.println(map2);

        // 转换成其他类型的List
        List<StudentInfo> studentInfoList1 = list1.stream().map(MapFlatMapTest::convertStuInfo).collect(Collectors.toList());
        System.out.println(studentInfoList1);

        List<StudentEntity> list2 = Lists.newArrayList(stu1, stu2);
        List<StudentEntity> list3 = Lists.newArrayList(stu3, stu4, stu5);

        // 符合类型的List中的List
        List<List<StudentEntity>> comList = Lists.newArrayList(list2, list3);
        List<StudentInfo> studentInfoList2 = comList.stream().flatMap(List::stream).map(MapFlatMapTest::convertStuInfo).collect(Collectors.toList());
        System.out.println(studentInfoList2);

        // 符合类型的List中的List转换Map
        Map<String, StudentInfo> map3 = comList.stream().flatMap(List::stream).map(MapFlatMapTest::convertStuInfo).collect(Collectors.toMap(StudentInfo::getUsername, Function.identity()));
        System.out.println(map3);

        // 循环判断
        System.out.println(list1.stream().anyMatch(s -> s.getAge() > 26));
        System.out.println(list1.stream().allMatch(s -> s.getAge() > 15));

        // 过滤
        System.out.println(list1.stream().filter(s -> s.getAge() > 25).collect(Collectors.toList()));

        // 排序
        System.out.println(list1.stream().sorted(Comparator.comparing(StudentEntity::getAge).thenComparing(StudentEntity::getGender)).collect(Collectors.toList()));
        System.out.println(list1.stream().sorted(Comparator.comparing(StudentEntity::getAge).reversed()).collect(Collectors.toList()));

    }

    private static StudentInfo convertStuInfo(StudentEntity s) {
        return  new StudentInfo(s.getUsername(), s.getGender());
    }

}

class StudentInfo {

    private String username;

    private String gender;

    public StudentInfo(String username, String gender) {
        this.username = username;
        this.gender = gender;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "StudentInfo{" +
                "username='" + username + '\'' +
                ", gender='" + gender + '\'' +
                '}';
    }
}

class StudentEntity {

    private String username;

    private String password;

    private String gender;

    private Integer age;

    public StudentEntity(String username, String password, String gender, Integer age) {
        this.username = username;
        this.password = password;
        this.gender = gender;
        this.age = age;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", gender='" + gender + '\'' +
                ", age='" + age + '\'' +
                '}';
    }
}
